//
//  HBBondDetailCtrl.m
//  HuoBao
//
//  Created by chaiyanan on 15/12/3.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBBondDetailCtrl.h"
#import "HBBondListProtocolCtrl.h"

@interface HBBondDetailCtrl ()

@property (nonatomic, strong) NSArray *titlesAry;
@property (nonatomic, strong) UILabel *nameLable;
@property (nonatomic, strong) UILabel *idNoLable;
@property (nonatomic, strong) UILabel *moneyLable;
@property (nonatomic, strong) UILabel *durationLable;
@property (nonatomic, strong) UILabel *overTimeLable;
@property (nonatomic, strong) UIButton *detailBtn;

@end

@implementation HBBondDetailCtrl

#pragma mark - system

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.titlesAry = @[@"借款人",@"身份证号",@"借款金额",@"借用周期",@"债权到期日",@"合同详情"];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.nameLable.text = self.contractItemInfo.borrowerName;
    self.idNoLable.text = self.contractItemInfo.borrowerCardno;
    self.moneyLable.text = self.contractItemInfo.loanMoney;
    self.durationLable.text = self.contractItemInfo.loanPeriod;
    self.overTimeLable.text = self.contractItemInfo.expireDate;
}

- (void)buildLayout
{
    [super buildLayout];
    self.title = @"债权详情";
    self.view.backgroundColor = [LCCommon getColor:@"e1e1e1"];
    
    CGFloat x = 21;
    CGFloat y = 10;
    CGFloat height = 50;
    CGFloat leftWidth = 100;
    CGFloat rightWidth = SCREEN_WIDTH - leftWidth - x*2;
    for (int i=0;i<self.titlesAry.count;i++)
    {
        BOOL isContainedBtn = NO;
        if (i==self.titlesAry.count-1)
        {
            isContainedBtn = YES;
        }
        UILabel *lable = [self createContentViewWithTitle:self.titlesAry[i] titleFont:[UIFont systemFontOfSize:16] titleColor:[LCCommon getColor:@"888888"] titleFrame:CGRectMake(x, y+i*height, leftWidth, height) value:@"" valueFont:[UIFont systemFontOfSize:16] valueColor:[LCCommon getColor:@"666666"] valueFrame:CGRectMake(leftWidth+x, y+i*height, rightWidth, height) isContainedBtn:isContainedBtn buttomLineFrame:CGRectMake(15, y+(i+1)*height-1/([UIScreen mainScreen].scale), SCREEN_WIDTH-30, 1/([UIScreen mainScreen].scale)) buttonFrame:CGRectMake(SCREEN_WIDTH-rightWidth-x, y+i*height, rightWidth, height)];
        switch (i)
        {
            case 0:
                self.nameLable = lable;
                break;
            case 1:
                self.idNoLable = lable;
                break;
            case 2:
                self.moneyLable = lable;
                break;
            case 3:
                self.durationLable = lable;
                break;
            case 4:
                self.overTimeLable = lable;
                break;
            default:
                break;
        }
    }
}

#pragma mark - private

- (UILabel *)createContentViewWithTitle:(NSString *)title
                              titleFont:(UIFont *)titleFont
                             titleColor:(UIColor *)titleColor
                             titleFrame:(CGRect)titleFrame
                                  value:(NSString *)value
                              valueFont:(UIFont *)valueFont
                             valueColor:(UIColor *)valueColor
                             valueFrame:(CGRect)valueFrame
                         isContainedBtn:(BOOL)isContainedBtn
                        buttomLineFrame:(CGRect)buttomLineFrame
                            buttonFrame:(CGRect)buttonFrame
{
    UILabel *titleLable = [[UILabel alloc]initWithFrame:titleFrame];
    titleLable.font = titleFont;
    titleLable.text = title;
    titleLable.textAlignment = NSTextAlignmentLeft;
    titleLable.textColor = titleColor;
    [self.view addSubview:titleLable];
    
    UIView *buttomLine = [[UIView alloc]initWithFrame:buttomLineFrame];
    buttomLine.backgroundColor = [LCCommon getColor:@"cfcfcf"];
    [self.view addSubview:buttomLine];
    
    if (!isContainedBtn)
    {
        UILabel *valueLable = [[UILabel alloc]initWithFrame:valueFrame];
        valueLable.font = valueFont;
        valueLable.text = value;
        valueLable.textAlignment = NSTextAlignmentRight;
        valueLable.textColor = valueColor;
        [self.view addSubview:valueLable];
        return valueLable;
    }
    else
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = buttonFrame;
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [button setImage:[UIImage imageNamed:@"bond_tosee"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickToSee) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    return nil;
}

#pragma mark - active

- (void)clickToSee
{
    HBBondListProtocolCtrl *protocolCtrl = [[HBBondListProtocolCtrl alloc]init];
    protocolCtrl.isFromBondDetail = YES;
    protocolCtrl.bondDetailUrl = self.contractItemInfo.contractUrl;
    [self.navigationController pushViewController:protocolCtrl animated:YES];
}

@end
